package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.VisitRecordDAO;
import com.zerosky.crm.po.VisitRecord;
import com.zerosky.crm.service.VisitRecordService;
import com.zerosky.crm.util.PageInfo;

/**
 * Visitrecord业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="visitRecordService")
public class VisitRecordServiceImpl implements VisitRecordService {

	@Autowired
	private VisitRecordDAO visitrecordDAO; // Visitrecord数据接口

	/**
	 * Visitrecord列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<VisitRecord> getVisitRecordList(Map map, PageInfo<VisitRecord> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<VisitRecord> list = visitrecordDAO.getVisitRecordList(map);
		page.setList(list);

		int count = visitrecordDAO.countVisitRecord(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 通过主键查找Visitrecord实体
	 * 
	 * @param id
	 *            Visitrecord主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public VisitRecord getVisitRecordById(String id) {
		return visitrecordDAO.getVisitRecordById(id);
	}

	/**
	 * 保存Visitrecord
	 * 
	 * @param Visitrecord
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveVisitRecord(VisitRecord visitrecord, String operation) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
			visitrecordDAO.addVisitRecord(visitrecord);
		} else if ("修改".equalsIgnoreCase(operation)) {
			visitrecordDAO.updateVisitRecord(visitrecord);
		}
	}

	/**
	 * 删除Visitrecord
	 * 
	 * @param idArray
	 *            Visitrecord主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteVisitRecordById(String id) throws Exception {
		visitrecordDAO.deleteVisitRecordById(id);
	}
}
